<?php
// +----------------------------------------------------------------------
// | Fanwe 方维订餐小秘书商业系统
// +----------------------------------------------------------------------
// | Copyright (c) 2011 http://www.fanwe.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: 云淡风轻(88522820@qq.com)
// +----------------------------------------------------------------------

class tviewModule extends StoreBaseModule
{	
	public function index()
	{
		$id = intval($_REQUEST['id']);
		
		if(check_ipop_limit(get_client_ip(),"recount_supplier_location",1200,$id))
		{
			recount_supplier_data_count($id,"tuan");
			recount_supplier_data_count($id,"youhui");
			recount_supplier_data_count($id,"daijin");
			recount_supplier_data_count($id,"event");
			recount_supplier_data_count($id,"shop");
		}
		$GLOBALS['tmpl']->caching = true;
		$cache_id  = md5(MODULE_NAME.ACTION_NAME.$id);		
		if (!$GLOBALS['tmpl']->is_cached('store_tview.html', $cache_id))	
		{	
			$store_info = $GLOBALS['db']->getRow("select * from ".DB_PREFIX."supplier_location where id = ".$id." and is_effect = 1");
			if(!$store_info)
			{
				showErr($GLOBALS['lang']['NO_STORE_INFO']);
			}		
			
			$store_info['group_point'] = $GLOBALS['db']->getAll("select id,name from ".DB_PREFIX."point_group as pg left join ".DB_PREFIX."point_group_link as pgl on pg.id = pgl.point_group_id  where pgl.category_id = ".$store_info['deal_cate_id']." order by sort asc" );
			foreach($store_info['group_point'] as $kk=>$vv)
			{
				$store_info['group_point'][$kk]['avg_point'] =  round(floatval($GLOBALS['db']->getOne("select avg_point from ".DB_PREFIX."supplier_location_point_result where supplier_location_id = ".$store_info['id']." and group_id = ".$vv['id'])),1);
			}
			
			
			$tags = array();
			if($store_info['tags']){
				$ttags = explode(" ",$store_info['tags']);
				foreach($ttags as $tv){
					$tags[$tv]['name'] =  $tv;
					$tags[$tv]['code'] = $tv;
				}
			}
			$store_info['tags_list'] = $tags;
			//标签分组
			$tag_group = $GLOBALS['db']->getAll("select g.id,g.name,g.allow_vote from ".DB_PREFIX."tag_group as g left join ".DB_PREFIX."tag_group_link as gl on g.id = gl.tag_group_id where gl.category_id = ".$store_info['deal_cate_id']." order by g.sort");
	
			foreach($tag_group as $k=>$v)
			{
				$tags = $GLOBALS['db']->getAll("select tag_name,total_count from ".DB_PREFIX."supplier_tag where group_id = ".$v['id']." and supplier_location_id =".$store_info['id']." order by total_count desc limit 30");
				$tags_arr = array();
				foreach($tags as $kk=>$vv)
				{
					$vv['tag_name'] = trim($vv['tag_name']);
					if($vv['tag_name']!='')
					{
						$tags_arr[$kk]['url'] = url("store","index",array("tag"=>$vv['tag_name']));
						$tags_arr[$kk]['name'] = $vv['tag_name'];
						$tags_arr[$kk]['total_count'] = $vv['total_count'];
					}
				}	
				$tag_group[$k]['tags'] = $tags_arr;
			}	
			
			$store_info['tag_group'] = $tag_group;

			$cate_item = $GLOBALS['db']->getRow("select * from ".DB_PREFIX."deal_cate where id = ".$store_info['deal_cate_id']);
			
			$site_nav[] = array('name'=>$GLOBALS['lang']['HOME_PAGE'],'url'=>url("index","index"));
			$site_nav[] = array('name'=>$GLOBALS['lang']['STORE_TAKEAWAY_LIST'],'url'=>url("store","index"));
			$site_nav[] = array('name'=>$cate_item['name'],'url'=>url("store","takeaway",array("cid"=>$cate_item['id'])));
			$site_nav[] = array('name'=>$store_info['name'],'url'=>url("store","tview",array("id"=>$store_info['id'])));
			$GLOBALS['tmpl']->assign("site_nav",$site_nav);
			
			//分类
			$store_cate_list = $GLOBALS['db']->getAll("SELECT dct.* FROM ".DB_PREFIX."deal_cate_type_location_link dctl LEFT JOIN ".DB_PREFIX."deal_cate_type dct ON dct.id =dctl.deal_cate_type_id WHERE dctl.location_id=".$id);
			foreach($store_cate_list as $kkk=>$vvv){
				$store_cate_list[$kkk]['url'] = url("store","index",array("cid"=>$store_info['deal_cate_id'],"tid"=>$vvv['id']));
			}
			$GLOBALS['tmpl']->assign("store_cate_list",$store_cate_list);
			
			$seo_title = $store_info['seo_title']?$store_info['seo_title']:$store_info['name'];
			$GLOBALS['tmpl']->assign("page_title",$seo_title);
			$seo_keyword = $store_info['seo_keyword']?$store_info['seo_keyword']:$store_info['name'];
			$GLOBALS['tmpl']->assign("page_keyword",$seo_keyword.",");
			$seo_description = $store_info['seo_description']?$store_info['seo_description']:$store_info['name'];
			$GLOBALS['tmpl']->assign("page_description",$seo_description.",");
			$GLOBALS['tmpl']->assign("store_info",$store_info);
			$GLOBALS['tmpl']->assign("show_share",true);
			$GLOBALS['tmpl']->assign("show_fav",true);
			
			$json_location_data[] =  $store_info;
			$json_location_data = json_encode($json_location_data);
			$GLOBALS['tmpl']->assign("json_location_data",$json_location_data);
			
			
			//输出最新加入的商家
			$new_stores = $GLOBALS['db']->getAll("select id,name,address from ".DB_PREFIX."supplier_location where city_id = ".$GLOBALS['deal_city']['id']." and is_effect = 1 order by id desc limit 5");
			$GLOBALS['tmpl']->assign("new_stores",$new_stores);
			$rec_stores = $GLOBALS['db']->getAll("select id,name,address,avg_point,dp_count from ".DB_PREFIX."supplier_location where city_id = ".$GLOBALS['deal_city']['id']." and is_recommend = 1 and is_effect = 1 order by is_recommend desc limit 5");
			$GLOBALS['tmpl']->assign("rec_stores",$rec_stores);
			
			//现金券（前代金）
			$xianjin_sql_count = "select count(*) from ".DB_PREFIX."deal as d left join ".DB_PREFIX."deal_location_link as l on l.deal_id = d.id where d.is_delete = 0 and d.is_effect = 1 and d.is_shop = 2 and d.time_status in (0,1) and l.location_id = ".$id;
			$xianjin_count = $GLOBALS['db']->getOne($xianjin_sql_count);
			if($xianjin_count){
				$xianjin_html = tviewModule::ajax_get_xianjin($xianjin_count);
				$GLOBALS['tmpl']->assign("xianjin_count",$xianjin_count);
				$GLOBALS['tmpl']->assign("xianjin_html",$xianjin_html);
			}
			
			//菜单
			$menu_count =  $GLOBALS['db']->getOne("SELECT count(*) FROM ".DB_PREFIX."supplier_location_menu WHERE supplier_location_id=".$id);
			if($menu_count > 0){
				$menu_html = tviewModule::ajax_get_menu($menu_count);;
				$GLOBALS['tmpl']->assign("menu_count",$menu_count);
				$GLOBALS['tmpl']->assign("menu_html",$menu_html);	
			}
			//订单
			$order_count = $GLOBALS['db']->getOne("SELECT count(*) FROM ".DB_PREFIX."supplier_location_order WHERE status in(0,1,2) AND sid=".$id);
			if($order_count > 0){
				$order_html = tviewModule::ajax_get_order($order_count);
				$GLOBALS['tmpl']->assign("order_count",$order_count);
				$GLOBALS['tmpl']->assign("order_html",$order_html);		
			}
		}
		
		$GLOBALS['tmpl']->display("store_tview.html",$cache_id);	
	}
	
	
	
	function get_reivew_form($arr){
		$store_info['id'] =  $arr['id'];
		$store_info['deal_cate_id'] =  $arr['deal_cate_id'];
		if(intval($_REQUEST['is_ajax'])==1){
			if(intval($_REQUEST['deal_cate_id'])> 0){
				$store_info['deal_cate_id'] = intval($_REQUEST['deal_cate_id']);
			}
			if(intval($_REQUEST['id'])>0){
				$store_info['id'] = intval($_REQUEST['id']);
			}
		}
		//点评的内容与分组
		$point_group = $GLOBALS['db']->getAll("select pg.name,pg.id from ".DB_PREFIX."point_group as pg left join ".DB_PREFIX."point_group_link as pgl on pg.id = pgl.point_group_id where pgl.category_id = ".intval($store_info['deal_cate_id'])." order by pg.sort asc");
		$store_info['point_group'] = $point_group;
		
		//点评标签分组
		$tag_group = $GLOBALS['db']->getAll("select tg.name,tg.id,tg.preset from ".DB_PREFIX."tag_group  as tg left join ".DB_PREFIX."tag_group_link as tgl on tg.id = tgl.tag_group_id where tgl.category_id = ".intval($store_info['deal_cate_id'])." and tg.allow_dp = 1 order by tg.sort asc");
		foreach($tag_group as $k=>$v)
		{
			$preset = $GLOBALS['db']->getOne("select mtg.preset from ".DB_PREFIX."supplier_tag_group_preset as mtg where mtg.supplier_location_id = ".$store_info['id']." and mtg.group_id = ".$v['id'] );
			if($preset)
			$tag_group[$k]['preset_tags'] = explode(" ",$preset);
			else
			$tag_group[$k]['preset_tags'] = explode(" ",$v['preset']);
		}
		$store_info['dp_tag_group'] = $tag_group;
		$GLOBALS['tmpl']->assign("store_info",$store_info);
		if(intval($_REQUEST['is_ajax'])==1)
			echo $GLOBALS['tmpl']->fetch("inc/review/review_form.html");
		else
			return $GLOBALS['tmpl']->fetch("inc/review/review_form.html");
	}
	
	public function ajax_get_xianjin($count=0){
				convert_req($_REQUEST);	
				if(intval($_REQUEST['id'])==0)
				return false;
				$location_id = intval($_REQUEST['id']);
				$page_size=4;
				$pageArgs = array();
				
				//分页
				$page = intval($_REQUEST['p']);
				if($page==0)
					$page = 1;
				
				$GLOBALS['tmpl']->assign("page",$page);
				$limit = (($page-1)*$page_size).",".$page_size;	
				require_once APP_ROOT_PATH.'app/Lib/page.php';
				
				$x_sql_count = "select count(*) from ".DB_PREFIX."deal as d 
					left join ".DB_PREFIX."deal_location_link as l on l.deal_id = d.id 
					where d.is_delete = 0 and d.is_effect = 1 and d.is_shop = 2 and d.time_status in (0,1) and l.location_id = ".$location_id;
				$count = $GLOBALS['db']->getOne($x_sql_count);
				
				$pageArgs['ctl'] = "tview";
				$pageArgs['act'] = "ajax_get_xianjin";
				$pageArgs['id'] = intval($_REQUEST['id']);
				$pageArgs['count'] = $count;
				$pageArgs['is_ajax'] = "1";
				$url_parameter = http_build_query($pageArgs);
				$page = new Page($count,$page_size,$url_parameter);   //初始化分页对象 		
				$p  =  $page->show();
				$GLOBALS['tmpl']->assign('pages',$p);
				
				$d_sql = "select id,name,img,uname,current_price,origin_price,buy_count from ".DB_PREFIX."deal as d 
					left join ".DB_PREFIX."deal_location_link as l on l.deal_id = d.id 
					where d.is_delete = 0 and d.is_effect = 1 and d.is_shop = 2 and d.time_status in (0,1) and l.location_id = ".$location_id." order by d.sort desc limit ".$limit;
				$result_list = $GLOBALS['db']->getAll($d_sql);
				//print_r($result_list);
				foreach($result_list as $k=>$v)
				{
					if($v['uname']=='')				
					$result_list[$k]['url'] = url("store","ydetail",array("id"=>$v['id']));	
					else
					$result_list[$k]['url'] = url("store","ydetail",array("id"=>$v['uname']));	
					$result_list[$k]['format_origin_price']=round($v['origin_price'],2);
					$result_list[$k]['format_current_price']=format_price($v['current_price']);	
					$result_list[$k]['discount'] = round(($v['current_price']/$v['origin_price'])*10,2);	
				}
				$GLOBALS['tmpl']->assign("result_list",$result_list);
				$html = $GLOBALS['tmpl']->fetch("inc/store/store_ajax_get_xianjin.html");
				if($_REQUEST['is_ajax']=="1")
					echo $html;
				else
					return $html;
	}
	
	public function ajax_get_order($count = 0){
		convert_req($_REQUEST);	
		if(intval($_REQUEST['id'])==0)
			return false;
		
		$pageArgs = array();
		
		$page_size = 15;
		
		if(!isset($_REQUEST['field']) || empty($_REQUEST['field'])){
			$field = "create_time";
		}
		else{
			$field = $_REQUEST['field'];
		}
		
		if(!isset($_REQUEST['sort']) || empty($_REQUEST['sort'])){
			$sort = "DESC";
		}
		else{
			$sort = $_REQUEST['sort'];
		}
		
		//分页
		$page = intval($_REQUEST['p']);
		if($page==0)
			$page = 1;
		
		$GLOBALS['tmpl']->assign("page",$page);
		$limit = (($page-1)*$page_size).",".$page_size;	
		require_once APP_ROOT_PATH.'app/Lib/page.php';
		
		if($count == 0)
		{
			$count = $GLOBALS['db']->getOne("SELECT count(*) FROM ".DB_PREFIX."takeaway_order WHERE status in(0,1) AND supplier_location_id=".intval($_REQUEST['id']));
		}
		$pageArgs['ctl'] = "view";
		$pageArgs['act'] = "ajax_get_order";
		$pageArgs['id'] = intval($_REQUEST['id']);
		$pageArgs['field'] = $field;
		$pageArgs['sort'] = $sort;
		$pageArgs['count'] = $count;
		$pageArgs['is_ajax'] = "1";
		$url_parameter = http_build_query($pageArgs);
		$page = new Page($count,$page_size,$url_parameter);   //初始化分页对象 		
		$p  =  $page->show();
		$GLOBALS['tmpl']->assign('pages',$p);
	
		$list = $GLOBALS['db']->getAll("SELECT * FROM ".DB_PREFIX."supplier_location_order WHERE status in(0,1,2) AND sid=".intval($_REQUEST['id'])." ORDER BY $field $sort LIMIT ".$limit);
		//一周内的订单
		$start_time = get_gmtime() - (7*24*3600);
		$end_time = get_gmtime();
		$new_count = $GLOBALS['db']->getOne("SELECT count(*) FROM ".DB_PREFIX."supplier_location_order WHERE status in(0,1,2) AND sid=".intval($_REQUEST['id'])." AND create_time BETWEEN $start_time AND $end_time");
		$GLOBALS['tmpl']->assign("new_count",$new_count);
		$GLOBALS['tmpl']->assign("count",$count);
		$GLOBALS['tmpl']->assign("list",$list);
		
		$GLOBALS['tmpl']->assign("sort",$sort == "DESC" ? "ASC" : "DESC");
		
		$GLOBALS['tmpl']->assign("field",$field);
		$GLOBALS['tmpl']->assign("id",$_REQUEST['id']);
		
		$html = $GLOBALS['tmpl']->fetch("inc/store/store_ajax_get_order.html");
		if($_REQUEST['is_ajax']=="1")
			echo $html;
		else
			return $html;
	}
	
	
	public function ajax_get_menu($count = 0){
		convert_req($_REQUEST);	
		if(intval($_REQUEST['id'])==0)
			return false;
		$supplier_location_id=intval($_REQUEST['id']);
		
		//输出菜单分类
		$menu_cate_list=load_auto_cache("cache_supplier_location_menu_cate",array("id"=>$supplier_location_id));			
		$GLOBALS['tmpl']->assign("menu_cate_list",$menu_cate_list);	
		//end
		
		$pageArgs = array();
		
		$page_size = 15;
		if(!isset($_REQUEST['field']) || empty($_REQUEST['field'])){
			$field = "id";
		}
		else{
			$field = $_REQUEST['field'];
		}
		
		if(!isset($_REQUEST['sort']) || empty($_REQUEST['sort'])){
			$sort = "DESC";
		}
		else{
			$sort = $_REQUEST['sort'];
		}
		
		
		$is_special = intval($_REQUEST['is_special']);
		$GLOBALS['tmpl']->assign("is_special",$is_special);
		if($is_special > 0){
			$ext_where = " AND is_special=1 ";
		}
		$cate_id = intval($_REQUEST['cate_id']);
		if($cate_id)
		{
			$ext_where = " AND is_special=0 AND cate_id=".$cate_id."";
			$GLOBALS['tmpl']->assign("cate_id",$cate_id);
		}
		
		if(trim($_REQUEST['keyword'])!='')
		{
			$ext_where = " AND (`name` like '%".trim($_REQUEST['keyword'])."%' OR `brief` like '%".trim($_REQUEST['keyword'])."%') ";
			$GLOBALS['tmpl']->assign("keyword",trim($_REQUEST['keyword']));
		}
		
		//分页
		$page = intval($_REQUEST['p']);
		if($page==0)
			$page = 1;
		
		$GLOBALS['tmpl']->assign("page",$page);
		$limit = (($page-1)*$page_size).",".$page_size;	
		require_once APP_ROOT_PATH.'app/Lib/page.php';
		
		$count = $GLOBALS['db']->getOne("SELECT count(*) FROM ".DB_PREFIX."supplier_location_menu WHERE supplier_location_id=".intval($_REQUEST['id'])." $ext_where ");
				
		$pageArgs['ctl'] = "view";
		$pageArgs['act'] = "ajax_get_menu";
		$pageArgs['id'] = intval($_REQUEST['id']);
		$pageArgs['cate_id'] = $cate_id;
		$pageArgs['is_special'] = $is_special;
		$pageArgs['field'] = $field;
		$pageArgs['sort'] = $sort;
		$pageArgs['count'] = $count;
		$pageArgs['is_ajax'] = "1";
		$pageArgs['keyword'] = addslashes(htmlspecialchars($_REQUEST['keyword']));
		$url_parameter = http_build_query($pageArgs);
		$page = new Page($count,$page_size,$url_parameter);   //初始化分页对象 		
		$p  =  $page->show();
		$GLOBALS['tmpl']->assign('pages',$p);
		
		$list = $GLOBALS['db']->getAll("SELECT * FROM ".DB_PREFIX."supplier_location_menu WHERE supplier_location_id=".intval($_REQUEST['id'])." $ext_where ORDER BY $field $sort  LIMIT ".$limit);
		
		$GLOBALS['tmpl']->assign("count",$count);
		$GLOBALS['tmpl']->assign("list",$list);
		
		$GLOBALS['tmpl']->assign("sort",$sort == "DESC" ? "ASC" : "DESC");
		
		$GLOBALS['tmpl']->assign("field",$field);
		$GLOBALS['tmpl']->assign("id",$_REQUEST['id']);
		
		$html = $GLOBALS['tmpl']->fetch("inc/store/store_ajax_get_menu.html");
		if($_REQUEST['is_ajax']=="1")
			echo $html;
		else
			return $html;
	}
	
	public function ajax_get_menu_nums(){
		$return = array();
		$ids = $_REQUEST['ids'];
		$ids = explode(",",$ids);
		$tmp = array();
		foreach($ids as $k=>$v){
			if(intval($v)>0){
				$tmp['id'] = $v;
				$GLOBALS['tmpl']->assign("id",$v); 
				$tmp['nums'] = es_session::get("cho_nums_".$v);
				$GLOBALS['tmpl']->assign("nums",$tmp['nums']); 
				$tmp['html'] = $GLOBALS['tmpl']->fetch("inc/store/res_cho_nums.html");
				$return[] =  $tmp;
			}
		}
		ajax_return($return);
	}
	
	public function ajax_set_menu_nums(){
		$return = array();
		$id = intval($_REQUEST['id']);
		$num = intval($_REQUEST['num']);
		es_session::set("cho_nums_".$id,$num); 
		$GLOBALS['tmpl']->assign("id",$id);
		$GLOBALS['tmpl']->assign("nums",$num); 
		$return['html'] = $GLOBALS['tmpl']->fetch("inc/store/res_cho_nums.html");
		ajax_return($return);
	}
	
	public function ajax_get_menu_float_all(){
		if(intval($_REQUEST['id'])==0)
			exit();
		$store_info = $GLOBALS['db']->getRowCached("select name from ".DB_PREFIX."supplier_location where id = ".intval($_REQUEST['id'])." and is_effect = 1");
		if(!$store_info)
			exit();
		
		$templist = $GLOBALS['db']->getAllCached("SELECT id,name,price,is_special FROM ".DB_PREFIX."supplier_location_menu WHERE supplier_location_id=".intval($_REQUEST['id'])." ORDER BY id DESC");
		$list = array();
		$special_num = 0;
		$normal_num = 0;
		$all_price = 0;
		foreach($templist as $k=>$v){
			$nums = es_session::get("cho_nums_".$v['id']);
			if(intval($nums) > 0){
				$templist[$k]['nums'] = $nums;
				if($v['is_special']){
					$list['special'][]=$templist[$k];
					$special_num +=1;
				}
				else{
					$list['normal'][]=$templist[$k];
					$normal_num +=1;
				}
				$all_price +=$v['price']*$nums;
			}
		}
		$GLOBALS['tmpl']->assign("store_info",$store_info);
		$GLOBALS['tmpl']->assign("special_num",$special_num);
		$GLOBALS['tmpl']->assign("normal_num",$normal_num);
		$GLOBALS['tmpl']->assign("all_price",$all_price);
		$GLOBALS['tmpl']->assign("list",$list);
		$GLOBALS['tmpl']->assign("sid",intval($_REQUEST['id']));
		$GLOBALS['tmpl']->display("inc/store/float_ctmenu.html");
	}
	
	public function menuorder(){
		convert_req($_REQUEST);
		$return = array("status"=>0,"message"=>"");
				
		if($GLOBALS['user_info']){
			$return['status']=1;
			$sid = intval($_REQUEST['sid']);
			if($sid==0){
				$return['status']=0;
				$return['message']="请选择餐厅!";
				ajax_return($return);
			}
			
			$store_info = $GLOBALS['db']->getRowCached("select takeaway_price,takeaway_nums,takeaway_tel from ".DB_PREFIX."supplier_location where id = ".$sid." and is_effect = 1");
			if(!$store_info)
				exit();
			
			$data['supplier_location_id'] =  $sid;
			$data['user_id'] = $GLOBALS['user_info']['id'];
			
			$templist = $GLOBALS['db']->getAllCached("SELECT id,name,price FROM ".DB_PREFIX."supplier_location_menu WHERE supplier_location_id=".$sid." ORDER BY id DESC");
			$list = array();
			$all_nums = 0;
			$all_price = 0;
			foreach($templist as $k=>$v){
				$nums = es_session::get("cho_nums_".$v['id']);
				if(intval($nums) > 0){
					$templist[$k]['nums'] = $nums;
					$list[]=$templist[$k];
					$all_nums += $nums;
					$all_price += $v['price'];
				}
			}
			
			if(floatval($all_price) < floatval($store_info['takeaway_price'])){
				$return['status']=0;
				$return['message']="起送价格不够哦!";
				ajax_return($return);
			}
			
			if(intval($all_nums) < intval($store_info['takeaway_nums'])){
				$return['status']=0;
				$return['message']="起送份数不够哦!";
				ajax_return($return);
			}
			
			$data['contacter'] = htmlspecialchars(addslashes(trim($_REQUEST['contacter'])));
			if($data['contacter']==""){
				$return['status']=0;
				$return['message']="请填写您的称呼!";
				ajax_return($return);
			}
			$data['sex'] = intval($_REQUEST['sex']);
			
			$data['tel'] = htmlspecialchars(addslashes(trim($_REQUEST['tel'])));
			if($data['tel']==""){
				$return['status']=0;
				$return['message']="请填写您的联系电话!";
				ajax_return($return);
			}
			$data['address'] = htmlspecialchars(addslashes(trim($_REQUEST['address'])));
			if($data['address']==""){
				$return['status']=0;
				$return['message']="请填写您的送餐地址!";
				ajax_return($return);
			}
			$data['msg'] = htmlspecialchars(addslashes(trim($_REQUEST['msg'])));
			$data['create_time'] = get_gmtime();
			
			if(!check_ipop_limit(get_client_ip(),"store_torder",intval(app_conf("SUBMIT_DELAY")))){
				ajax_return(array("status"=>false,"message"=>$GLOBALS['lang']['SUBMIT_TOO_FAST']));
			}
			
			if($list){
				$data['cache_data'] = addslashes(serialize($list));
				//数据
				do
				{
					$data['order_sn'] = to_date(get_gmtime(),"Ymdhis").rand(10,99);
					$GLOBALS['db']->autoExecute(DB_PREFIX."takeaway_order",$data,'INSERT','','SILENT'); 
					$order_id = intval($GLOBALS['db']->insert_id());
				}while($order_id==0);
				
				if($order_id > 0){
					foreach($list as $k=>$v){
						$nums = es_session::get("cho_nums_".$v['id']);
						if(intval($nums) > 0){
							$GLOBALS['db']->query("UPDATE ".DB_PREFIX."supplier_location_menu SET collect_nums = collect_nums + 1 WHERE id=".$v['id']);
						}
						es_session::delete("cho_nums_".$v['id']);
					}
					
					
					//发短信
					if(app_conf("SMS_SEND_TAKEAWAY_STORE")==1 && $store_info['takeaway_tel']!=''){
						$msg= "菜单：";
						foreach($list as $kk=>$vv){
							if($kk!=0)
								$msg .=",";
							$msg .= $vv['name']."×".$vv['nums'];
						}
						
						$msg .="。联系人:".$data['contacter'];
						$msg .=",联系电话:".$data['tel'];
						$msg .=",送餐地址：".$data['address'];
						if($data['msg'])
							$msg .=",备注：".$data['msg'];
						$msg .= "。";
						
						$msg_data['send_type'] = 0;
						$msg_data['content'] = addslashes($msg);
						$msg_data['send_time'] = 0;
						$msg_data['is_send'] = 0;
						$msg_data['create_time'] = get_gmtime();
						$msg_data['user_id'] = intval($GLOBALS['user_info']['id']);
						$msg_data['is_html'] = 0;
						$msg_data['is_youhui'] = 4;
						$msg_data['dest'] = $store_info['takeaway_tel'];
						$GLOBALS['db']->autoExecute(DB_PREFIX."deal_msg_list",$msg_data,"INSERT"); //插入 
					}
					
					$return['status']=1;
					ajax_return($return);
				}
				else{
					$return['status']=0;
					$return['message']="处理数据失败!";
					ajax_return($return);
				}		
			}
			else{
				$return['status']=1;
				ajax_return($return);
			}
		}
		else{
			$return['status']=2;
			ajax_return($return);
		}
	}
	
	function load_recent_sign($param)
	{
		$id = intval($param['id']);
		$time = get_gmtime();
		$begin_time = get_gmtime() - 3 * 24 * 3600;
		$end_time = get_gmtime() + 3 * 24 * 3600;

		$list = $GLOBALS['db']->getAll("select distinct(user_id) from ".DB_PREFIX."supplier_location_sign_log where location_id = $id and (sign_time between $begin_time and $end_time) order by sign_time desc limit 9 ");
		$count = $GLOBALS['db']->getOne("select count(distinct(user_id)) from ".DB_PREFIX."supplier_location_sign_log where location_id = $id and (sign_time between $begin_time and $end_time)  ");
		$GLOBALS['tmpl']->assign("list",$list);
		$GLOBALS['tmpl']->assign("count",$count);
		if($list)
		return $GLOBALS['tmpl']->fetch("inc/user_sign.html");
		else
		return ""; 
		
	}
	
}
?>